<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-03-21
 * Time: 17:12
 */

class Tag extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 标签分类列表
     */
    public function category_list(){
        $pageTitle = "分类列表";
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        $pagesize = $this->input->get_post('pagesize') ? $this->input->get_post('pagesize') : 20;

        $where = array("level" => 1);
        $orderBy = array("id" => "asc");
        $categoryList = $this->TagModel->getList($where, $page, $pagesize, $orderBy);
        $num = $this->TagModel->getListNum($where);

        $url = 'index.php?c=tag&m=category_list';
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('tag/category_list',array(
            "pageTitle" => $pageTitle,
            "categoryList" => $categoryList,
            "multipage" => $multipage,
            "page" => $page
        ));
    }

    /**
     * 编辑标签
     */
    public function tag_edit(){
        $pageTitle = '编辑';
        $tag_id = $this->input->get_post('tag_id',true);
        $type = $this->input->get_post('type',true) ? $this->input->get_post('type',true) : 1; // 1:标签分类 2:标签列表
        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        if($_REQUEST["submits"]){
            $param = $this->input->get_post('tag',true);
            $param["level"] = $type;
            if(empty($tag_id)){
                $this->TagModel->insert_one($param);
                $this->AdminOpenationModel->insertLog($type,19,'添加标签：'.json_encode($param));
            }else{
                $this->TagModel->update_data($param, array("id" => $tag_id));
                $this->AdminOpenationModel->insertLog($type,20,'修改标签：'.json_encode($param));
            }
            if($type == 1){
                showMessage("index.php?c=tag&m=category_list&page=".$page);
            }else{
                showMessage("index.php?c=tag&m=tag_list&page=".$page);
            }
        }
        $tag = array();
        if(!empty($tag_id)){
            $tag = $this->TagModel->query_one(array("id"=>$tag_id));
        }
        $categoryList = array();
        if($type == 2){
            $where = array("status" => 1, "level" => 1);
            $orderBy = array("id" => "asc");
            $categoryList = $this->TagModel->getList($where, 0, 0, $orderBy);
        }
        $this->load->view('tag/edit_tag',array(
            'pageTitle'=>$pageTitle,
            "tag" => $tag,
            "tag_id" => $tag_id,
            "type" => $type,
            "categoryList" => $categoryList,
            "page" => $page
        ));
    }

    /**
     * 设置标签的状态
     */
    public function set_status(){
        $id = $this->input->get_post('id',true);
        $status = $this->input->get_post('status',true);
        if(empty($id) || empty($status)){
            std_out(-1, "非法请求", "");
        }
        $param = array("status" => $status);
        $this->TagModel->update_data($param, array("id" => $id));
        $this->AdminOpenationModel->insertLog($id,21,'设置标签的状态：'.$id);
        std_out(0, "");
    }
    /**
     * 标签列表
     */
    public function tag_list(){
        $pageTitle = "标签列表";

        $page = $this->input->get_post('page') ? $this->input->get_post('page') : 1;
        $pagesize = $this->input->get_post('pagesize') ? $this->input->get_post('pagesize') : 20;

        $where = array("level" => 2);
        $orderBy = array("id" => "asc");
        $tagList = $this->TagModel->getList($where, $page, $pagesize, $orderBy);
        $num = $this->TagModel->getListNum($where);

        $url = 'index.php?c=tag&m=tag_list';
        $multipage = multipage($num, $page, $pagesize, $url);

        $this->load->view('tag/tag_list',array(
            "pageTitle" => $pageTitle,
            "tagList" => $tagList,
            "multipage" => $multipage,
            "page" => $page
        ));
    }

    /**
     * ajax查询列表
     * name: get_all_topic
     * @throws Exception
     * @Author:taiyang
     */
    public function get_all_tag(){
        $name = $this->input->get_post('tag_name') ? $this->input->get_post('tag_name') : '';
        $list=$this->TagModel->getTagList($name);
        std_out(0,'',$list);
    }
}